#!/bin/sh

. scripts/functions

#
# Run a simple backup with encryption and a real sparse file.
#
TestName="sparse-encrypt-test"
JobName=SparseTest

scripts/cleanup
scripts/copy-crypto-confs

# Directory to backup.
# This directory will be created by setup_data().
BackupDirectory="${tmp}/data"

# Use a tgz to setup data to be backed up.
# Data will be placed at "${tmp}/data/".
setup_data data/small.tgz

# the default fileset FS_TESTJOB backups all file and directories defined in "${tmp}/file-list".
echo "${BackupDirectory}" >${tmp}/file-list

SPARSEFILE="${BackupDirectory}/gigaslam.gif"

start_test

# create a sparse file
scripts/create_sparse_file.sh ${SPARSEFILE} 100M
if [ $? != 0 ]; then
    set_error "create_sparse_file failed."
    rm -f ${SPARSEFILE}
    exit 1
fi

if [ ! -e "${SPARSEFILE}" ]; then
    set_error "Failed to create sparse file."
    exit 1
fi

cat >${cwd}/tmp/bconcmds <<END_OF_DATA
@$out /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File volume=TestVolume001
setdebug level=10 fd
run job=$JobName yes
wait
messages
list volumes
@#
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
@# setdebug level=0 fd
restore where=${cwd}/tmp/susan-restores storage=File
5
mark *
done
yes
wait
messages
quit
END_OF_DATA

run_susan
check_for_zombie_jobs storage=File
stop_susan

check_two_logs
check_restore_diff "${BackupDirectory}"

size_orig=`get_file_size ${SPARSEFILE}`
size=`get_file_size ${cwd}/tmp/susan-restores/${SPARSEFILE}`
if [ -z "$size" ]; then
    set_error "Failed to determine size of restored sparse file."
# TODO: encrypted sparse files are not handled as sparse files.
#elif [ $size -gt 120 ]; then
#    set_error "Restored sparse file ${SPARSEFILE} is too big (${size}K > 120K)"
fi

print_debug "size orig:     $size_orig"
print_debug "size restored: $size"

rm -f ${SPARSEFILE}

end_test
